<template>
	<el-dialog
		:title="titleMap[mode]"
		v-model="visible"
		:width="700"
		destroy-on-close
		append-to-body
		@closed="emits('closed')"
	>
		<el-form
			:model="form"
			:rules="rules"
			ref="dialogForm"
			label-width="80px"
			:style="'el-dialog' === 'el-drawer' ? 'padding:0 20px' : ''"
		>

			<el-row :gutter="20">
				<el-col :span="12">
					<el-form-item label="客户属性" prop="ke_hu_shu_xing">
						<el-select v-model="form.ke_hu_shu_xing" style="width:100%" clearable placeholder="请选择客户属性">

							<el-option
								v-for="(item, index) in dictData.wms_cus_weight_type"
								:key="index" :label="item.label"
								:value="item.value"
							>{{ item.label }}
							</el-option>

						</el-select>
					</el-form-item>
				</el-col>
				<el-col :span="12">
					<el-form-item label="客户编码" prop="ke_hu_bian_ma">
						<el-input v-model="form.ke_hu_bian_ma" clearable placeholder="请输入客户编码"/>
					</el-form-item>
				</el-col>
			</el-row>

			<el-row :gutter="20">
				<el-col :span="12">
					<el-form-item label="企业属性" prop="xing_ye_fen_lei">
						<el-select v-model="form.xing_ye_fen_lei" style="width:100%" clearable placeholder="请选择企业属性">

							<el-option
								v-for="(item, index) in dictData.wms_enterprise_attributes"
								:key="index" :label="item.label"
								:value="item.value"
							>{{ item.label }}
							</el-option>

						</el-select>
					</el-form-item>
				</el-col>
				<el-col :span="12">
					<el-form-item label="手机" prop="shou_ji">
						<el-input v-model="form.shou_ji" clearable placeholder="请输入手机"/>
					</el-form-item>
				</el-col>
			</el-row>

			<el-row :gutter="20">
				<el-col :span="12">
					<el-form-item label="负责人" prop="zhu_lian_xi_ren">
						<el-input v-model="form.zhu_lian_xi_ren" clearable placeholder="请输入负责人"/>
					</el-form-item>
				</el-col>
				<el-col :span="12">
					<el-form-item label="地址" prop="di_zhi">
						<el-input v-model="form.di_zhi" clearable placeholder="请输入地址"/>
					</el-form-item>
				</el-col>
			</el-row>

			<el-row :gutter="20">
				<el-col :span="12">
					<el-form-item label="中文全称" prop="zhong_wen_qch">
						<el-input v-model="form.zhong_wen_qch" clearable placeholder="请输入中文全称"/>
					</el-form-item>
				</el-col>
				<el-col :span="12">
					<el-form-item label="营业执照号" prop="ying_ye_zhi_zhao">
						<el-input v-model="form.ying_ye_zhi_zhao" clearable placeholder="请输入营业执照号"/>
					</el-form-item>
				</el-col>
			</el-row>

			<el-row :gutter="20">
				<el-col :span="12">
					<el-form-item label="所属部门" prop="sys_org_code">
						<el-input v-model="form.sys_org_code" clearable placeholder="请输入所属部门"/>
					</el-form-item>
				</el-col>
				<el-col :span="12">
					<el-form-item label="所属公司" prop="sys_company_code">
						<el-input v-model="form.sys_company_code" clearable placeholder="请输入所属公司"/>
					</el-form-item>
				</el-col>
			</el-row>

			<el-row :gutter="20">
				<el-col :span="12">
					<el-form-item label="助记码" prop="zhu_ji_ma">
						<el-input v-model="form.zhu_ji_ma" clearable placeholder="请输入助记码"/>
					</el-form-item>
				</el-col>
				<el-col :span="12">
					<el-form-item label="客户简称" prop="ke_hu_jian_cheng">
						<el-input v-model="form.ke_hu_jian_cheng" clearable placeholder="请输入客户简称"/>
					</el-form-item>
				</el-col>
			</el-row>

			<el-row :gutter="20">
				<el-col :span="12">
					<el-form-item label="客户英文名称" prop="ke_hu_ying_wen">
						<el-input v-model="form.ke_hu_ying_wen" clearable placeholder="请输入客户英文名称"/>
					</el-form-item>
				</el-col>
				<el-col :span="12">
					<el-form-item label="曾用企业代码" prop="zeng_yong_qi">
						<el-input v-model="form.zeng_yong_qi" clearable placeholder="请输入曾用企业代码"/>
					</el-form-item>
				</el-col>
			</el-row>

			<el-row :gutter="20">
				<el-col :span="12">
					<el-form-item label="曾用企业名称" prop="zeng_yong_qi_ye">
						<el-input v-model="form.zeng_yong_qi_ye" clearable placeholder="请输入曾用企业名称"/>
					</el-form-item>
				</el-col>
				<el-col :span="12">
					<el-form-item label="客户等级" prop="ke_hu_deng_ji">
						<el-input v-model="form.ke_hu_deng_ji" clearable placeholder="请输入客户等级"/>
					</el-form-item>
				</el-col>
			</el-row>

			<el-row :gutter="20">
				<el-col :span="12">
					<el-form-item label="所属行业" prop="suo_shu_xing_ye">
						<el-input v-model="form.suo_shu_xing_ye" clearable placeholder="请输入所属行业"/>
					</el-form-item>
				</el-col>
				<el-col :span="12">
					<el-form-item label="首签日期" prop="shou_qian_ri_qi">
						<el-input v-model="form.shou_qian_ri_qi" clearable placeholder="请输入首签日期"/>
					</el-form-item>
				</el-col>
			</el-row>

			<el-row :gutter="20">
				<el-col :span="12">
					<el-form-item label="终止合作时间" prop="zhong_zhi_he_shi_jian">
						<el-date-picker
							v-model="form.zhong_zhi_he_shi_jian" placeholder="请选择终止合作时间"
							type="datetime"
							value-format="YYYY-MM-DD HH:mm:ss"
						/>
					</el-form-item>
				</el-col>
				<el-col :span="12">
					<el-form-item label="归属组织代码" prop="gui_shu_zu_zh">
						<el-input v-model="form.gui_shu_zu_zh" clearable placeholder="请输入归属组织代码"/>
					</el-form-item>
				</el-col>
			</el-row>

			<el-row :gutter="20">
				<el-col :span="12">
					<el-form-item label="归属省份代码" prop="gui_shu_sheng">
						<el-input v-model="form.gui_shu_sheng" clearable placeholder="请输入归属省份代码"/>
					</el-form-item>
				</el-col>
				<el-col :span="12">
					<el-form-item label="归属市代码" prop="gui_shu_shi_dai">
						<el-input v-model="form.gui_shu_shi_dai" clearable placeholder="请输入归属市代码"/>
					</el-form-item>
				</el-col>
			</el-row>

			<el-row :gutter="20">
				<el-col :span="12">
					<el-form-item label="归属县区代码" prop="gui_shu">
						<el-input v-model="form.gui_shu" clearable placeholder="请输入归属县区代码"/>
					</el-form-item>
				</el-col>
				<el-col :span="12">
					<el-form-item label="邮政编码" prop="you_zheng_bian_ma">
						<el-input v-model="form.you_zheng_bian_ma" clearable placeholder="请输入邮政编码"/>
					</el-form-item>
				</el-col>
			</el-row>

			<el-row :gutter="20">
				<el-col :span="12">
					<el-form-item label="电话" prop="dian_hua">
						<el-input v-model="form.dian_hua" clearable placeholder="请输入电话"/>
					</el-form-item>
				</el-col>
				<el-col :span="12">
					<el-form-item label="传真" prop="chuan_zhen">
						<el-input v-model="form.chuan_zhen" clearable placeholder="请输入传真"/>
					</el-form-item>
				</el-col>
			</el-row>

			<el-row :gutter="20">
				<el-col :span="12">
					<el-form-item label="Email地址" prop="Emaildi_zhi">
						<el-input v-model="form.Emaildi_zhi" clearable placeholder="请输入Email地址"/>
					</el-form-item>
				</el-col>
				<el-col :span="12">
					<el-form-item label="网页地址" prop="wang_ye_di_zhi">
						<el-input v-model="form.wang_ye_di_zhi" clearable placeholder="请输入网页地址"/>
					</el-form-item>
				</el-col>
			</el-row>

			<el-row :gutter="20">
				<el-col :span="12">
					<el-form-item label="法人代表" prop="fa_ren_dai_biao">
						<el-input v-model="form.fa_ren_dai_biao" clearable placeholder="请输入法人代表"/>
					</el-form-item>
				</el-col>
				<el-col :span="12">
					<el-form-item label="法人身份证号" prop="fa_ren_shen_fen">
						<el-input v-model="form.fa_ren_shen_fen" clearable placeholder="请输入法人身份证号"/>
					</el-form-item>
				</el-col>
			</el-row>

			<el-row :gutter="20">
				<el-col :span="12">
					<el-form-item label="注册资金万元" prop="zhu_ce_zi_jin">
						<el-input v-model="form.zhu_ce_zi_jin" clearable placeholder="请输入注册资金万元"/>
					</el-form-item>
				</el-col>
				<el-col :span="12">
					<el-form-item label="币别" prop="bi_bie">
						<el-input v-model="form.bi_bie" clearable placeholder="请输入币别"/>
					</el-form-item>
				</el-col>
			</el-row>

			<el-row :gutter="20">
				<el-col :span="12">
					<el-form-item label="税务登记证号" prop="shui_wu_deng">
						<el-input v-model="form.shui_wu_deng" clearable placeholder="请输入税务登记证号"/>
					</el-form-item>
				</el-col>
				<el-col :span="12">
					<el-form-item label="组织机构代码证" prop="zu_zhi_ji_gou">
						<el-input v-model="form.zu_zhi_ji_gou" clearable placeholder="请输入组织机构代码证"/>
					</el-form-item>
				</el-col>
			</el-row>

			<el-row :gutter="20">
				<el-col :span="12">
					<el-form-item label="道路运输经营许可证" prop="dao_lu_yun_shu">
						<el-input v-model="form.dao_lu_yun_shu" clearable placeholder="请输入道路运输经营许可证"/>
					</el-form-item>
				</el-col>
				<el-col :span="12">
					<el-form-item label="主营业务" prop="zhu_ying_ye_wu">
						<el-input v-model="form.zhu_ying_ye_wu" clearable placeholder="请输入主营业务"/>
					</el-form-item>
				</el-col>
			</el-row>

			<el-row :gutter="20">
				<el-col :span="12">
					<el-form-item label="合作意向" prop="he_yi_xiang">
						<el-input v-model="form.he_yi_xiang" clearable placeholder="请输入合作意向"/>
					</el-form-item>
				</el-col>
				<el-col :span="12">
					<el-form-item label="批准机关" prop="pi_zhun_ji_guan">
						<el-input v-model="form.pi_zhun_ji_guan" clearable placeholder="请输入批准机关"/>
					</el-form-item>
				</el-col>
			</el-row>

			<el-row :gutter="20">
				<el-col :span="12">
					<el-form-item label="批准文号" prop="pi_zhun_wen_hao">
						<el-input v-model="form.pi_zhun_wen_hao" clearable placeholder="请输入批准文号"/>
					</el-form-item>
				</el-col>
				<el-col :span="12">
					<el-form-item label="注册日期" prop="zhu_ce_ri_qi">
						<el-date-picker
							v-model="form.zhu_ce_ri_qi" placeholder="请选择注册日期"
							type="datetime"
							value-format="YYYY-MM-DD HH:mm:ss"
						/>
					</el-form-item>
				</el-col>
			</el-row>

			<el-row :gutter="20">
				<el-col :span="12">
					<el-form-item label="备注" prop="bei_zhu">
						<el-input v-model="form.bei_zhu" clearable placeholder="请输入备注"/>
					</el-form-item>
				</el-col>
				<el-col :span="12">
					<el-form-item label="联系人1" prop="zhu_lian_xi_ren1">
						<el-input v-model="form.zhu_lian_xi_ren1" clearable placeholder="请输入联系人1"/>
					</el-form-item>
				</el-col>
			</el-row>

			<el-row :gutter="20">
				<el-col :span="12">
					<el-form-item label="电话1" prop="dian_hua1">
						<el-input v-model="form.dian_hua1" clearable placeholder="请输入电话1"/>
					</el-form-item>
				</el-col>
				<el-col :span="12">
					<el-form-item label="状态" prop="status">
						<el-switch v-model="form.status" :active-value="proxy.$CONFIG.SWITCH.ENABLE"
											 :inactive-value="proxy.$CONFIG.SWITCH.DISABLE"/>
					</el-form-item>
				</el-col>
			</el-row>

			<el-row :gutter="20">
				<el-col :span="12">
					<el-form-item label="排序" prop="sort">
						<el-input v-model="form.sort" clearable placeholder="请输入排序"/>
					</el-form-item>
				</el-col>
			</el-row>

		</el-form>
		<template #footer>
			<el-button @click="visible = false">取 消</el-button>
			<el-button type="primary" :loading="isSaveing" @click="submit()">保 存</el-button>
		</template>
	</el-dialog>
</template>

<script setup>
import {ref, reactive, defineExpose, onMounted, getCurrentInstance} from 'vue'
import {useRouter} from "vue-router";
import {ElMessage} from 'element-plus'
import wmsMdCus from '@/api/apis/wms/wmsMdCus'
import systemDict from '@/api/apis/system/dataDict'


const emits = defineEmits(['success', 'closed'])
const {proxy} = getCurrentInstance();
const router = useRouter();

const mode = ref('add')
const treeList = ref([])
const visible = ref(false)
const isSaveing = ref(false)
const dialogForm = ref(null)

const titleMap = reactive({add: '新增客户信息', edit: '编辑客户信息'})
const dictData = reactive({
	platform_data_status: [],
	wms_cus_weight_type: [],
	wms_enterprise_attributes: [],

})
const form = reactive({
	id: '',
	sys_org_code: '',
	sys_company_code: '',
	zhu_ji_ma: '',
	ke_hu_jian_cheng: '',
	ke_hu_ying_wen: '',
	zeng_yong_qi: '',
	zeng_yong_qi_ye: '',
	ke_hu_deng_ji: '',
	suo_shu_xing_ye: '',
	shou_qian_ri_qi:null,
	zhong_zhi_he_shi_jian:null,
	gui_shu_zu_zh: '',
	gui_shu_sheng: '',
	gui_shu_shi_dai: '',
	gui_shu: '',
	you_zheng_bian_ma: '',
	dian_hua: '',
	chuan_zhen: '',
	Emaildi_zhi: '',
	wang_ye_di_zhi: '',
	fa_ren_dai_biao: '',
	fa_ren_shen_fen: '',
	zhu_ce_zi_jin: '',
	bi_bie: '',
	shui_wu_deng: '',
	zu_zhi_ji_gou: '',
	dao_lu_yun_shu: '',
	zhu_ying_ye_wu: '',
	he_yi_xiang: '',
	pi_zhun_ji_guan: '',
	pi_zhun_wen_hao: '',
	zhu_ce_ri_qi: null,
	bei_zhu: '',
	zhu_lian_xi_ren1: '',
	dian_hua1: '',
	status: proxy.$CONFIG.SWITCH.ENABLE,
	sort: '0',
	shou_ji: '',
	mer_id: '0',
	zhu_lian_xi_ren: '',
	di_zhi: '',
	ying_ye_zhi_zhao: '',
	zhong_wen_qch: '',
	ke_hu_shu_xing: '1',
	ke_hu_bian_ma: '',
	xing_ye_fen_lei: '1',

})
const rules = reactive({
	shou_ji: [{required: true, message: '手机必填', trigger: 'blur'}],
	zhu_lian_xi_ren: [{required: true, message: '负责人必填', trigger: 'blur'}],
	di_zhi: [{required: true, message: '地址必填', trigger: 'blur'}],
	zhong_wen_qch: [{required: true, message: '中文全称必填', trigger: 'blur'}],
	ke_hu_shu_xing: [{required: true, message: '客户属性必填', trigger: 'blur'}],
	ke_hu_bian_ma: [{required: true, message: '客户编码必填', trigger: 'blur'}],
	xing_ye_fen_lei: [{required: true, message: '企业属性必填', trigger: 'blur'}],

})

onMounted(async () => {
	await getDictData()
	if (router.currentRoute.value.query.open) {
		open('add');
	}
})

const getDictData = () => {
	systemDict.getDict('platform_data_status').then(res => {
		dictData.platform_data_status = res.data
	})
	systemDict.getDict('wms_cus_weight_type').then(res => {
		dictData.wms_cus_weight_type = res.data
	})
	systemDict.getDict('wms_enterprise_attributes').then(res => {
		dictData.wms_enterprise_attributes = res.data
	})

}

const open = (type = 'add') => {
	mode.value = type
	visible.value = true

}

const submit = () => {
	dialogForm.value.validate(async (valid) => {
		if (valid) {
			isSaveing.value = true
			const res = mode.value === 'add' ? await wmsMdCus.save(form) : await wmsMdCus.update(form.id, form)
			if (res.success) {
				emits('success', form, mode.value)
				visible.value = false
				ElMessage.success(res.message)
			} else {
				ElMessage.error(res.message)
			}
			isSaveing.value = false
		}
	})
}

const setData = (data) => {
	for (let k in form) {
		if (data[k] || data[k] === 0) {
			form[k] = data[k]
		}
	}
}


defineExpose({
	open, setData
})
</script>
